package com.ccjq.shosestore.member.dao.impl;

import com.ccjq.shosestore.core.dbmanger.DbConnection;
import com.ccjq.shosestore.member.dao.UserDao;
import com.ccjq.shosestore.member.po.UserModel;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 用户持久层操作具体实现类定义
 */
public class UserDaoImpl implements UserDao {

    private Connection con = null;
    private PreparedStatement pre = null;
    private ResultSet res = null;

    public int save(UserModel model) {
        int result = 0;
        String sql = "insert into t_user values(?,?,?,?)";
        try {
            con = DbConnection.getConnection();
            pre = con.prepareStatement(sql);
            pre.setString(1, model.getUserId());
            pre.setString(2, model.getUsername());
            pre.setString(3, model.getPassword());
            pre.setString(4, model.getMobilePhone());
            result = pre.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbConnection.close(null, pre, con);
        }
        return result;
    }

    public int update(UserModel model) {
        int result = 0;
        String sql = "update t_user set username=?,password=?,mobile_phone=? where user_id=?";
        try {
            con = DbConnection.getConnection();
            pre = con.prepareStatement(sql);
            pre.setString(1, model.getUsername());
            pre.setString(2, model.getPassword());
            pre.setString(3, model.getMobilePhone());
            pre.setString(4, model.getUserId());
            result = pre.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbConnection.close(null, pre, con);
        }
        return result;
    }

    public int delete(String userId) {
        int result = 0;
        String sql = "delete from  t_user  where user_id=?";
        try {
            con = DbConnection.getConnection();
            pre = con.prepareStatement(sql);
            pre.setString(1, userId);
            result = pre.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbConnection.close(null, pre, con);
        }
        return result;
    }

    public UserModel findUserById(String userId) {
        UserModel usermodel = null;
        String sql = "select user_id,username,password,mobile_phone from t_user where  user_id=?";
        try {
            con = DbConnection.getConnection();
            pre = con.prepareStatement(sql);
            pre.setString(1, userId);
            res = pre.executeQuery();
            if (res.next()) {
                usermodel = new UserModel();
                usermodel.setUserId(res.getString(1));
                usermodel.setUsername(res.getString(2));
                usermodel.setPassword(res.getString(3));
                usermodel.setMobilePhone(res.getString(4));
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbConnection.close(res, pre, con);
        }
        return usermodel;
    }

    public UserModel findUserByNamePw(String username, String password) {
        UserModel usermodel = null;
        String sql = "select user_id,username,password,mobile_phone from t_user where username=? and password=?";
        try {
            con = DbConnection.getConnection();
            pre = con.prepareStatement(sql);
            pre.setString(1, username);
            pre.setString(2, password);
            res = pre.executeQuery();
            if (res.next()) {
                usermodel = new UserModel();
                usermodel.setUserId(res.getString(1));
                usermodel.setUsername(res.getString(2));
                usermodel.setPassword(res.getString(3));
                usermodel.setMobilePhone(res.getString(4));
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbConnection.close(res, pre, con);
        }
        return usermodel;
    }

    public UserModel findUserByName(String username) {
        UserModel usermodel = null;
        String sql = "select user_id,username,password,mobile_phone from t_user where username=?";
        try {
            con = DbConnection.getConnection();
            pre = con.prepareStatement(sql);
            pre.setString(1, username);
            res = pre.executeQuery();
            if (res.next()) {
                usermodel = new UserModel();
                usermodel.setUserId(res.getString(1));
                usermodel.setUsername(res.getString(2));
                usermodel.setPassword(res.getString(3));
                usermodel.setMobilePhone(res.getString(4));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbConnection.close(res, pre, con);
        }
        return usermodel;
    }
}
